package com.example.testapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

// 系统广播
public class ImoocBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "ImoocBroadcastReceiver";
    TextView mTextView;
    public ImoocBroadcastReceiver() {
    }

    public ImoocBroadcastReceiver(TextView textView) {
        mTextView = textView;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // 接收广播
        if(intent != null){
            // 接收到是什么广播
            String action = intent.getAction();
            // ImoocBroadcastReceiver: onReceive:android.intent.action.PACKAGE_REMOVED
            Log.d(TAG,"onReceive:" + action);

            // 用来监听设备网络状态
            if(action.equals(ConnectivityManager.EXTRA_NO_CONNECTIVITY)){}

            // 判断是什么广播
            if(TextUtils.equals(action,BroadcastReceiverActivity.MY_ACTION)){
                // 获取广播携带的内容，可自定义的数据
                String content = intent.getStringExtra(BroadcastReceiverActivity.BROADCAST_CONTENT);
                if(mTextView != null){
                    mTextView.setText("接收到的action是:" + action + "\n接收的内容是:\n" + content);
                }
            }

        }



    }
}
